//
//  ContentListCell.m
//  yigou
//
//  Created by SunChao on 17/1/11.
//  Copyright © 2017年 hanyiwang. All rights reserved.
//

#import "TopicListCell.h"
#import "contentModel.h"

#define nameFont [UIFont systemFontOfSize:12]
#define textFont [UIFont systemFontOfSize:13]
#define timeFont [UIFont systemFontOfSize:10]
#define replyFont [UIFont systemFontOfSize:13]
@implementation TopicListCell
-(void)awakeFromNib
{
    self.selectionStyle = UITableViewCellSelectionStyleNone;   //cell选中时的颜色，无色
    //用户头像
    self.userImg.image = [UIImage imageNamed:@""];
    //用户名
    self.userName.textColor = [UIColor colorWithRed:77/255.0 green:182/255.0 blue:172/255.0 alpha:1];
    //部落名称
    self.tribeName.textColor = [UIColor colorWithRed:77/255.0 green:182/255.0 blue:172/255.0 alpha:1];
    //关注按钮
    [self.guanzhuBtn setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    //创建正文
    self.shuoshuotextLabel.font = textFont;
    self.shuoshuotextLabel.textColor = [UIColor colorWithRed:77/255.0 green:77/255.0 blue:77/255.0 alpha:1];
    self.shuoshuotextLabel.numberOfLines = 0;
    //创建时间戳
    self.publish_tim.font = timeFont;
    self.publish_tim.textColor = [UIColor colorWithRed:152/255.0 green:152/255.0 blue:152/255.0 alpha:1];
        //创建评论按钮
        [self.replyButton setImage:[UIImage imageNamed:@"reply"] forState:0];
        //创建评论的背景
//        UIImageView *replyBackgroundView = [[UIImageView alloc]init];
//        replyBackgroundView.backgroundColor = [UIColor colorWithRed:242/255.0 green:242/255.0 blue:242/255.0 alpha:1.0];
//        [self.contentView addSubview:replyBackgroundView];
//        self.replyBackgroundView = replyBackgroundView;
    
        [self removeOldPicturesAndReplys];
        [self settingtData];
//        [self settingFrame];
}
//+(instancetype)cellWithTableView:(UITableView *)tableView
//{
//    static NSString *identifier = @"FamilyGroupCell";
//    
//    //缓存中取
//    TopicListCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
//    
//    //创建
//    if (!cell)
//    {
//        cell = [[TopicListCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
//    }
//    //cell.userInteractionEnabled = NO;  //不让用户点击
//    return cell;
//}
-(void)contentWithDic:(NSDictionary *)contentDic
{
    contentModel *model = [[contentModel alloc]initWithDictionary:contentDic error:nil];
    [self.userImg sd_setImageWithURL:[NSURL URLWithString:model.head_url] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
    self.userImg.layer.cornerRadius = 20;
    self.userImg.layer.masksToBounds = YES;
    self.userName.text = model.nick_name;
    self.tribeName.text = model.tribe_name;
//    [self.converImg sd_setImageWithURL:[NSURL URLWithString:model.cover_img] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
//    self.title.text = model.title;
//    self.contentDesc.text = model.content_desc;
    
    //发布时间
    self.publish_tim.text = model.publish_tm;
    //评论
    if (model.plCount == 0) {
        self.plCount.frame = CGRectMake(0, 0, 0, 0);
    }else{
        self.plCount.text = [NSString stringWithFormat:@"%ld",(long)model.plCount];
    }
    //点赞
    if (model.likeCount == 0) {
        self.likeCount.frame = CGRectMake(0, 0, 0, 0);
        self.likeImg.image = [UIImage imageNamed:@"nice"];
    }else{
        self.likeCount.text = [NSString stringWithFormat:@"%ld",(long)model.likeCount];
        self.likeImg.image = [UIImage imageNamed:@"点赞"];
    }
    //收藏
    if (model.iscollect == NO) {
        self.scImg.image = [UIImage imageNamed:@"收藏"];
    }
    self.scImg.image = [UIImage imageNamed:@"Group 12 Copy 2"];
    //分享
    self.shareImg.image = [UIImage imageNamed:@"share"];
    
}
-(void)settingtData
{
//    FamilyGroup *familyGroup = self.familyGroupFrame.familyGroup;
//    
//    //创建头像
//    self.iconView.image = [UIImage imageNamed:familyGroup.icon];
//    
//    //创建昵称
//    self.nameLabel.text = familyGroup.name;
//    
//    //创建正文
//    self.shuoshuotextLabel.text = familyGroup.shuoshuoText;
//    
//    //创建配图
//    for (int i = 0; i < [familyGroup.pictures count]; i++) {
//        UIImageView *pictureView = [[UIImageView alloc]init];
//        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapImageView:)];
//        [pictureView addGestureRecognizer:tap];
//        pictureView.tag = imageTag + i;
//        //NSLog(@"%ld",(long)pictureView.tag);
//        pictureView.userInteractionEnabled = YES;
//        [self.contentView addSubview:pictureView];
//        [self.picturesView addObject:pictureView];
//        ((UIImageView *)[self.picturesView objectAtIndex:i]).image = [UIImage imageNamed:[familyGroup.pictures objectAtIndex:i]];
//    }
//    
//    //时间戳
//    self.timeLabel.text = familyGroup.time;
//    
//    //创建评论
//    for (int i = 0; i < [familyGroup.replys count]; i++) {
//        UILabel *replyLabel = [[UILabel alloc]init];
//        replyLabel.font = replyFont;
//        replyLabel.numberOfLines = 0;
//        
//        NSString *searchText = [familyGroup.replys objectAtIndex:i];
//        //NSString *searchText = @"浮夸：哈哈";
//        //把评论的姓名变色，用正则表达式
//        NSRange range = [searchText rangeOfString:@"([\u4e00-\u9fa5]|[a-zA-Z0-9])+：" options:NSRegularExpressionSearch];
//        if (range.location != NSNotFound) {
//            NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:searchText];
//            [str addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:104/255.0 green:109/255.0 blue:248/255.0 alpha:1.0] range:NSMakeRange(0, range.length - 1)];
//            replyLabel.attributedText = str;
//        }
//        else
//        {
//            replyLabel.text = [familyGroup.replys objectAtIndex:i];
//        }
//        [self.contentView addSubview:replyLabel];
//        [self.replysView addObject:replyLabel];
//    }
    
}
-(NSMutableArray *)picturesView
{
    if (!_picturesView) {
        _picturesView = [[NSMutableArray alloc]init];
    }
    return _picturesView;
    
}

-(NSMutableArray *)replysView
{
    if (!_replysView) {
        _replysView = [[NSMutableArray alloc]init];
    }
    return _replysView;
}
//-(void)settingFrame
//{
//    self.iconView.frame = self.familyGroupFrame.iconF;
//    
//    self.nameLabel.frame = self.familyGroupFrame.nameF;
//    
//    self.shuoshuotextLabel.frame = self.familyGroupFrame.shuoshuotextF;
//    
//    for (int i = 0; i < [self.familyGroupFrame.picturesF count]; i++) {
//        ((UIImageView *)[self.picturesView objectAtIndex:i]).frame = [((NSValue *)[self.familyGroupFrame.picturesF objectAtIndex:i]) CGRectValue];
//    }
//    for (int i = 0; i < [self.familyGroupFrame.replysF count]; i++) {
//        ((UILabel *)[self.replysView objectAtIndex:i]).frame = [(NSValue *)[self.familyGroupFrame.replysF objectAtIndex:i] CGRectValue];
//    }
//    self.timeLabel.frame = self.familyGroupFrame.timeF;
//    
//    self.replyButton.frame = self.familyGroupFrame.replyF;
//    
//    self.replyBackgroundView.frame = self.familyGroupFrame.replyBackgroundF;
//}

//-(void)setFamilyGroupFrame:(FamilyGroupFrame *)familyGroupFrame
//{
//    _familyGroupFrame = familyGroupFrame;
//   
//}

//防止cell重叠
-(void)removeOldPicturesAndReplys
{
    for(int i = 0;i < [self.picturesView count];i++)
    {
        UIImageView *pictureView = [self.picturesView objectAtIndex:i];
        if (pictureView.superview) {
            [pictureView removeFromSuperview];
        }
    }
    for (int i = 0; i < [self.replysView count]; i++) {
        UILabel *replyView = [self.replysView objectAtIndex:i];
        if (replyView.superview) {
            [replyView removeFromSuperview];
        }
    }
    [self.picturesView removeAllObjects];
    [self.replysView removeAllObjects];
}

//-(void)tapImageView:(UITapGestureRecognizer *)tap
//{
//    self.imageBlock(self.familyGroupFrame.familyGroup.pictures,tap.view.tag);
//}
-(void)setFrame:(CGRect)frame
{
    frame.size.height-=15;
    [super setFrame:frame];
}


@end
